package com.joyhonest.rulerview;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.joyhonest.ruler.PeriodTime;
import com.joyhonest.ruler.RulerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private Timer moveTimer;
    private RulerView rulerView;
    private SeekBar seekBar;
    private boolean zoomThroughRuler = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView currentTime = findViewById(R.id.currentTime);
        rulerView = findViewById(R.id.rulerView);
        rulerView.setCurrentCalendar(2021, 2, 25);
        rulerView.setOnRulerChangeListener(new RulerView.OnRulerChangeListener() {
            @Override
            public void onTimeChange(Calendar calendar) {
                String time = String.format("%4d:%02d:%02d %02d : %02d : %02d",
                        calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
                        calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
                currentTime.setText(time);
            }

            @Override
            public void onZoomChange(int percent) {
                seekBar.setProgress(percent);
            }
        });

        //设置时间段
        Calendar startCalendar1 = Calendar.getInstance();
        startCalendar1.set(2021, 2, 25, 0, 0, 0);
        Calendar stopCalendar1 = Calendar.getInstance();
        stopCalendar1.set(2021, 2, 25, 0, 0, 40);
        PeriodTime periodTime1 = new PeriodTime(startCalendar1, stopCalendar1);

        Calendar startCalendar2 = Calendar.getInstance();
        startCalendar2.set(2021, 2, 25, 0, 2, 0);
        Calendar stopCalendar2 = Calendar.getInstance();
        stopCalendar2.set(2021, 2, 25, 0, 5, 0);
        PeriodTime periodTime2 = new PeriodTime(startCalendar2, stopCalendar2);

        Calendar startCalendar3 = Calendar.getInstance();
        startCalendar3.set(2021, 2, 25, 2, 0, 0);
        Calendar stopCalendar3 = Calendar.getInstance();
        stopCalendar3.set(2021, 2, 25, 5, 0, 0);
        PeriodTime periodTime3 = new PeriodTime(startCalendar3, stopCalendar3);

        List<PeriodTime> videoList = new ArrayList<>();
        videoList.add(periodTime1);
        videoList.add(periodTime2);
        videoList.add(periodTime3);
        rulerView.setPeriodTimeList(videoList);

        //外部驱动向前移动
        Button goForward = findViewById(R.id.goForward);
        goForward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goForward();
            }
        });

        //外部驱动向后移动
        Button goBack = findViewById(R.id.goBack);
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();
            }
        });

        //停止外部驱动的移动
        Button stopMove = findViewById(R.id.stopMove);
        stopMove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopMove();
            }
        });

        seekBar = findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int percent, boolean b) {
                if (!zoomThroughRuler) {
                    rulerView.scale(percent);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                zoomThroughRuler = false;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                zoomThroughRuler = true;
            }
        });
    }

    private void goForward() {
        if (moveTimer != null) {
            moveTimer.cancel();
            moveTimer = null;
        }
        moveTimer = new Timer();
        moveTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                rulerView.goForward(1);
            }
        }, 0, 1000);
    }

    private void goBack() {
        if (moveTimer != null) {
            moveTimer.cancel();
            moveTimer = null;
        }
        moveTimer = new Timer();
        moveTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                rulerView.goBack(1);
            }
        }, 0, 1000);
    }

    private void stopMove() {
        if (moveTimer != null) {
            moveTimer.cancel();
            moveTimer = null;
        }
    }
}